Skip to content

feat(toolbox-core): Add support for optional parameters #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

anubhav756
Copy link
Contributor

@anubhav756 anubhav756 commented Jun 19, 2025

Background

The previous implementation of toolbox-core treated all tool parameters as implicitly required. This limited the flexibility of tool definitions and did not align with common use cases where tools have optional arguments with default behaviors.

This PR introduces full support for optional parameters, corresponding to the feature added to the Toolbox server (#617), enabling developers to create more versatile and powerful tools. The changes ensure that optionality is correctly reflected and handled at every stage: from the function signature and type hinting down to the final payload sent to the server.

Proposed Changes

This PR introduces a series of coordinated changes across the parameter handling and tool invocation pipeline:

1. ParameterSchema Enhancement

  • A new required: bool = True field has been added to the ParameterSchema of the Toolbox protocol, allowing tool manifest authors to explicitly mark parameters as optional.
  • The internal __get_type() method now correctly wraps the type in typing.Optional when a parameter has required=False.
  • The to_param() method now constructs an inspect.Parameter with default=None for optional parameters.

2. Correct Signature Generation

  • In the ToolboxTool.__init__ method, the list of tool parameters is now sorted to place all required parameters before optional ones.
    • This robustly prevents a ValueError: non-default argument follows default argument that could occur if a manifest defined an optional parameter before a required one.

3. Aligned Pydantic Validation

The params_to_pydantic_model utility function has been updated to be aware of the required flag. It now generates Pydantic Field definitions with default=None for optional fields, ensuring runtime validation is consistent with the function signature.

4. Cleaner API Payloads

  • The payload now only includes parameters that were explicitly provided by the user in the function call.
  • Optional parameters that are omitted will be completely left out of the JSON request body, rather than being sent with a null value.
    • The Toolbox server fills such a field with the user-provided default value.

Impact

  • For Tool Authors: You can now define optional parameters in your tool manifests by setting `default: "Some Value".
  • For Tool Users: Tools with optional parameters will now have correct, user-friendly Python signatures (e.g., my_tool(required_arg: str, optional_arg: Optional[str] = "Some Value")). Standard tools like help()` and editor autocompletion will work as expected.
  • For API Integrity: Requests sent to the Toolbox server are cleaner and more concise, reducing ambiguity by omitting keys for unprovided optional values.

@anubhav756 anubhav756 self-assigned this Jun 19, 2025
@anubhav756 anubhav756 requested review from kurtisvg and Yuan325 June 19, 2025 07:41
@anubhav756 anubhav756 marked this pull request as ready for review June 19, 2025 08:08
@anubhav756 anubhav756 requested a review from a team as a code owner June 19, 2025 08:08
@anubhav756
Copy link
Contributor Author

/gcbrun

kurtisvg
kurtisvg previously approved these changes Jun 19, 2025
kurtisvg
kurtisvg previously approved these changes Jun 20, 2025
@anubhav756 anubhav756 force-pushed the anubhav-optional-params branch from c31eb0c to d2d2027 Compare June 24, 2025 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants